var main=[[ //表头
	{type:'checkbox', fixed: 'left'},
	{field: 'auction_no', title: '拍卖单号',  sort: true, fixed: 'left'}
    ,{field: 'number', title: '车牌号',  sort: true,templet:`<div>{{d.number?d.number:"-"}}</div>`}
    ,{field: 'brand', title: '品牌',templet:`<div>{{d.brand?d.brand:"-"}}</div>`} 
    ,{field: 'car_type_name', title: '车型' ,templet:`<div>{{d.car_type_name?d.car_type_name:"-"}}</div>`}
    ,{field: 'company_name', title: '公司',templet:`<div>{{d.company_name?d.company_name:"-"}}</div>`}
    ,{field:'state2', title:'状态'}
   	,{ title: '操作',width:250, toolbar:'#operation',fixed: 'right'}
]];
var auto='';
var fi=1;
var page=1;
var data = {};
data.page = page;
data.pagenum = 10;
var where_json = {};
	{if $Think.get.car_audit_id}
		where_json.car_audit_id=['like',"%{$Think.get.car_audit_id}%"];
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like',"%{$Think.get.company_name}%"];
	{/if}
	{if $Think.get.number}
		where_json.number=['like',"%{$Think.get.number}%"];
	{/if}
	{if $Think.get.state}
		where_json.state=['in',"{$Think.get.state}"];
	{/if}
	data.where_json = JSON.stringify(where_json)
	ajaxauction(data)
	function ajaxauction(data) {
		Load()
	ajax(api.cycle_auction.list, data, 'post').done(function(res) {
		console.log(res)
		Load(1)
		if(res.code == 1) {
			for(var stateidx = 0; stateidx < res.data.length; stateidx++) {
				if(res.data[stateidx].state == 1) {
					res.data[stateidx].state2 = '待审批'
				} else if(res.data[stateidx].state == 2) {
					res.data[stateidx].state2 = '审批中'
				} else if(res.data[stateidx].state == 3) {
					res.data[stateidx].state2 = '已拒绝'
				} else if(res.data[stateidx].state == 4) {
					res.data[stateidx].state2 = '待确认'
				} else if(res.data[stateidx].state == 5) {
					res.data[stateidx].state2 = '待登记'
				} else if(res.data[stateidx].state == 6) {
					res.data[stateidx].state2 = '已完成'
				} 
			}
			table.render({
    				elem: '#buy',
    				cellMinWidth: 80,
    				cols: main,
    				data: res.data,
    				limit: data.pagenum,
    				even: true
    			});
    			if (fi == 1) {
    				laypage.render({
    					elem: 'page',
    					count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
    					jump: function (obj, first) {
    						if (!first) {
    							page = obj.curr
    							data.page = page
    							pagenum = obj.limit
    							data.pagenum = pagenum
    							ajaxbuycar(data)
    						} else {

    						}
    					}
    				});
    				fi = 2
    			}
    		table.on('tool(buy)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'detail') {
					detail_auction(data1)
				}else if(layEvent === 'check_auction1'){
					check_auction(data1,1)
				}else if(layEvent === 'check_auction2'){ //审核
					check_auction(data1,2)
				}else if(layEvent === 'auction_affirm'){ //确认
					check_auction(data1,3)
				}else if(layEvent === 'auction_file_image'){ //确认
					sumbit_auction(data1,1)
				}else if(layEvent === 'auction_file_finance_'){ //确认
					sumbit_auction(data1,2)
				}else if(layEvent === 'auction_delete'){ //删除
					delete_auction(data1)
				}else if(layEvent === 'look'){ //删除
					window.location.href='/html/car/auction_detail?car_auction_id='+data1.car_auction_id+'&car_id='+data1.car_id
				}
				
			})
    		
			
			
			
		} else {
			Err('请求失败')
		}
	}).fail(

	)
}
$('.add').on('click',function(res){
	propcheck()
})

	//申请
	function propcheck(obj,main){
		var auto = layer.open({
   		type: 1,
   		title:'车辆拍卖申请',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['650px', '350px'], //宽高
   		content: `
   			<div class='layui-row' style='padding:10px'>
   			<form class="layui-form">
	   			<div class='layui-col-sm12'>
	   				<div  style='margin-bottom:10px'>
		   				<div class="layui-form-item " style="padding-right:10px">
		   				<div class="layui-input-inline">
		   					<input type="hidden" class="car_id" name="car_id" value="{$item.car_id}" />
							<input type="text" class="layui-input number" value="{$item.car_number}" name="car_number" data-check='require' data-msg="车牌号不能为空" readonly="" />
		   				</div>
		   				<div class='layui-btn-group am-fl layui-btn-group-xs cl-p' >
		   					<button type="button" class='layui-btn  car_detail'>查看车辆详情</button>
		   				</div>
	   					</div>
	   				</div>
	   				<div >
		   				<div class='layui-col-sm12'>
			   				<textarea style='height:150px;width:100%;padding:10px' class='why' placeholder='请填写拍卖理由'></textarea>
		   				</div>
	   				</div>
	   				
	   			</div>
	   			
	   			<div class='layui-col-sm12' style="text-align:center;margin-top:10px">
	   					<button type='button' class='layui-btn apply_ok'>提交申请</button>
	   					<button type='button' class='layui-btn apply_no'>取消</button>
	   			</div>
	   		</form>
   			</div>
   		`,
   		success:function(){
   			$('.number').on('click',function(){
				opsearch("{:url('/html/car/car')}?op=search")
			});
   			$('.car_detail').on('click',function(){
   				opsearch("{:url('/html/car/car')}?op=search")
//				   console.log($('.car_id').val()=="")
// 				if($('.car_id').val()==""){
//					Err('请选择车辆')
//				}else{
//					opsearch("{:url('/html/car/car_detail')}?op=search2&car_id=" + $('.car_id').val());
////					choosecar("{:url('/html/car/car_detail')}?car_id="+$('.CarId').val(),'车辆详情')
//	   			}
   			})
   			$('.apply_ok').on('click',function(){
	   			Load()
	   			ajax(api.cycle_auction.add,{
	   				car_id:$('.car_id').val(),
	   				why:$('.why').val()
	   			},'post').done(function(res){
	   				Load(1)
	   				if(res.code==1){
	   					console.log(res)
						Msg('已提交申请')
						ajaxauction(data)
						layer.close(auto)
					}else{
						Err('请求失败')
					}
	   			}).fail(function(){
	   				Load(1)
	   				Err('请求失败')
	   			})
	   			
	   			})
	   			$('.apply_no').on('click',function(){
	   				layer.close(auto)
	   			})
   		}
   		})
	}
	//删除	
	function delete_auction(obj,main){
		auto = layer.open({
	   		type: 1,
	   		title:false,
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['200px', '100px'], //宽高
	   		content: `
	   			<div class='layui-col-sm12' style='padding:10px'>
	   				<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>是否删除该拍卖单</div>
	   				<div class='layui-col-sm12' style='text-align:center'>
	   					<a class='layui-btn layui-btn-sm  am-radius close_ok'>是</a>
	   					<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_no'>否</a>
	   				</div>
	   			</div>
	   		`,
	   		success:function(){
	   			
	   			$('.close_ok').on('click',function(){
	   				Load()
	   				ajax(api.cycle_auction.delete,{
	   						car_auction_id:obj.car_auction_id
	   					},'post').done((res)=>{
	   						Load(1)
	   						console.log(res)
	   						if(res.code==1){
	   							ajaxauction(data)
	   							Msg('删除成功')
	   						}
	   						layer.close(auto)
	   					}).fail(()=>{
	   						Load(1)
	   						Err('请求失败')
	   						layer.close(auto)
	   					})
	   				
	   			});
	   			$('.close_no').on('click',function(){
	   				layer.close(auto)
	   			})
	   		}
	   	})
	}
	//审批
	function check_auction(obj,type){
		let check_data={
			car_auction_id:obj.car_auction_id,		
		}
		let main='';
		if(type==3){
			main='是否确认该车已完成拍卖'
		}else{
			main='是否同意该车辆进行拍卖'
		}
		auto = layer.open({
			type: 1,
			title:false,
			skin: 'layui-layer-rim', //加上边框
			area: ['300px', '150px'], //宽高
			content: `
				<div class='layui-col-sm12' style='padding:10px'>
					<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>${obj.number} <button type="button" class='layui-btn layui-btn-sm  car_detail'>查看车辆详情</button></div>
					<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>${main}</div>
					<div class='layui-col-sm12' style='text-align:center'>
						<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_ok'>同意</a>
						<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_no'>拒绝</a>
					</div>
				</div>
			`,
			success:function(){
				$('.car_detail').on('click',function(){
					window.open("{:url('html/car/car_detail')}?_blank=1&car_id="+obj.car_id);  
				})
				$('.close_ok').on('click',function(){
					if(type==1){
						check_data.bureau_of_finance_result=1
					}else if(type==2){
						check_data.provincial_administration_result=1
					}else if(type==3){
						check_data.provincial_administration_again_result=1
					}
					Load()
					ajax(api.cycle_auction.check,check_data,'post').done((res)=>{
						console.log(res)
						Load(1)
						if(res.code==1){
							ajaxauction(data)
							layer.close(auto)
						}else{
							if(res.msg){
								Err(res.msg)
							}
							
						}
					}).fail(()=>{
						Load(1);
						Err('请求失败')
					})
				})
				$('.close_no').on('click',function(){
					if(type==1){
						check_data.bureau_of_finance_result=9
					}else if(type==2){
						check_data.provincial_administration_result=9
					}else if(type==3){
						check_data.provincial_administration_again_result=9
					}
					Load()
					ajax(api.cycle_auction.check,check_data,'post').done((res)=>{
						console.log(res)
						Load(1)
						if(res.code==1){
							ajaxauction(data)
							layer.close(auto)
						}else{
							Err(res.msg)
						}
					}).fail(()=>{
						Load(1);
						Err('请求失败')
					})
				})
			}
				
				
		})
	}
	
	//上传
	function sumbit_auction(obj,type){
		let file={}
		if(type==1){
			file.name='上传拍卖图片';
			file.key='auction_image';
		}else{
			file.name='上传资产处理图片';
			file.key='bureau_of_finance_file';
		}
		auto = layer.open({
			type: 1,
			title:false,
			skin: 'layui-layer-rim', //加上边框
			area: ['500px', '350px'], //宽高
			content: `
				<div style='padding:10px'>
				<form class="layui-form" >
					<div class='layui-row'>
						
						<div class='layui-form-item'>
						<label class="layui-form-label">拍卖金额:</label>
							<div class="layui-input-inline">
								<input class='layui-input' name='auction_money'/>
							</div>
							
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">是否上缴国库</label>
							<div class="layui-input-block">
								<input type="checkbox" name="turn_over" lay-skin="switch" lay-text="ON|OFF">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">图片上传</label>
							<div class="layui-input-block">
								<div >
									<div class="layui-upload "  style='float:left;padding-right:10px;text-align:center'>
										<div class="layui-upload-list">
											<img class="layui-upload-img auction_image" data-key='auction_image' id="auction_image">
											<p id="demoText"></p>
										</div>
										<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id='auction_image-btn'>上传拍卖图片</button>
									</div>
									<div class="layui-upload " style='float:left;text-align:center'>
										<div class="layui-upload-list">
											<img class="layui-upload-img bureau_of_finance_file" data-key='bureau_of_finance_file' id="bureau_of_finance_file">
											<p id="demoText"></p>
										</div>
										<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" id='bureau_of_finance_file-btn'>上传资产处理图片</button>
									</div>
								</div>
							</div>
						</div>
						<div style='text-align:center'>
						<button  class="layui-btn" lay-submit lay-filter="file_ok">确认上传</button>
						</div>
					</div>
					</form>
				</div>
			`,
			success:function(){
				form.render(); 
				let filedata={}
				upImage(api.upimage.upimage, '#auction_image-btn', '#auction_image', filedata,'auction_image');
				upImage(api.upimage.upimage, '#bureau_of_finance_file-btn', '#bureau_of_finance_file', filedata,'bureau_of_finance_file');
				
				 form.on('submit(file_ok)', function(data1){
				 	console.log(filedata)
				 	for(var key in filedata){
				 		data1.field[key]=filedata[key]
				 	}
				 	
					 data1.field.car_auction_id=obj.car_auction_id;
				   if(data1.field.turn_over=='on'){
				   	data1.field.turn_over=1
				   }else{
				   	data1.field.turn_over=9
				   }
				   Load()
				   console.log()
				   delete data1.field.image
				   ajax(api.cycle_auction.check,data1.field,'post').done((res)=>{
				   	console.log(res)
						Load(1)
						if(res.code==1){
							ajaxauction(data)
							layer.close(auto)
						}else{
							Err(res.msg?res.msg:'请求失败')
						}
				   }).fail(()=>{
				   	Load(1);
						Err('请求失败')
				   })
				    return false;
				  });
			}
				
				
		})
	}
//})
	function detail_auction(obj){
		console.log(obj)
		auto = layer.open({
			type: 1,
			title: false,
			skin: 'layui-layer-rim', //加上边框
			area: ['400px', '500px'], //宽高
			content: `
				<div class='layui-row'>
					<div class='layui-col-sm12'>
						<div class=' m-label'>拍卖单号:</div>
						<div class='m-value'>${obj.auction_no}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>车牌号:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>拍卖文件上传时间:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>拍卖文件:</div>
						<div class='m-value'>
							<img src='${obj.auction_image}' style='height:100px;width:100px'>
						</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>资产批复上传时间:</div>
						<div class='m-value'>${obj.number}</div>
					</div>
					<div class='layui-col-sm12'>
						<div class=' m-label'>资产批复文件:</div>
						<div class='m-value'>
							<img src='${obj.bureau_of_finance_file}' style='height:100px;width:100px'>
						</div>
					</div>
					
				</div>
			`

		})
	}